package alg20250104;

public class Text3 {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        //1 创建表
        int[][] dp = new int[2][n];
        //2 初始化
        dp[2][0] = -prices[0];
        //3 填表
        for (int i = 1; i < n; i++) {
            dp[0][i] = Math.max(dp[0][i - 1], dp[1][i - 1]);
            dp[1][i] = dp[2][i - 1] + prices[i];
            dp[2][i] = Math.max(dp[2][i - 1], dp[0][i - 1] - prices[i]);
        }
        //4 返回值
        return Math.max(dp[0][n - 1], dp[1][n -1]);
    }
}
